import requests
import json
import time
import random
import hashlib
from bs4 import BeautifulSoup
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
    'Referer': 'http://sso.jwc.whut.edu.cn/Certification/toLogin.do',
}
def getwebfigner():
    ls=('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0')
    str=""
    for temp in range(32):
        str+=ls[random.randint(0,35)]
    return str
def getcode(webfinger,session):
    code_url="http://sso.jwc.whut.edu.cn/Certification/getCode.do"
    data={
        'webfinger':webfinger
    }
    page=session.post(code_url,headers=headers,data=data)
    return page.text

def signIN_sso(sno, password):
    url_log = 'http://sso.jwc.whut.edu.cn/Certification/login.do'
    post_data = {
        'userName': sno,
        'password': password,
        'type': 'xs',
        'MsgID': "",
        "KeyID": "",
        "rnd":"",
        "UserName": "",
        "Password": "",
        "return_EncData": "",
        "code": "",
        "userName1": "",
        "password1": "",
        "webfinger": "",

    }
    # get userName && passWord
    m=hashlib.md5()
    m.update(sno.encode("utf8"))
    post_data['userName1']=m.hexdigest()
    m=hashlib.sha1()
    m.update((sno+password).encode("utf8"))
    post_data['password1']=m.hexdigest()
    post_data['webfinger']=getwebfigner()
    s = requests.Session()
    page=s.get(url="http://sso.jwc.whut.edu.cn/Certification/toLogin.do",headers=headers)
    soup=BeautifulSoup(page.text,"html.parser")
    post_data['rnd']=str(soup.find("input",id="rnd")['value'])
    post_data['code']=getcode(post_data['webfinger'],s)
    test = s.post(url_log, data=post_data, headers=headers)
    print(test.text)


    soup=BeautifulSoup(test.text,"html.parser")
    jse_url=soup.find_all("ul",class_="clearfix")[3].find_all("a")[0]['href']
    s.get(url=jse_url, headers=headers)
    s.close()
    return jse_url,s.cookies


def get_data(jse_url,cookies):
    """
    post_data的信息从谷歌浏览器拿到
    :param cookies:
    :return:
    """
    post_data = {
        'xnxq': '2018-2019-2',
        'kcdm': '4220015001',#爱情心理学
        'jxjhh': '2018',
        'addid': '796FC4CF17E7147DE053FD02A8C0B4BE',
        'keyinfo': '1B287D9EDB89287477F83965F974C3BB',
        '_': ""
    }
    url = jse_url+"/gxkxkAdd.do"
    s = requests.session()
    s.cookies = cookies
    error = 300
    while error == 300:
        try:
            tim = int(time.time())
            post_data['_'] = str(tim)
            page = s.get(url=url, params=post_data)
            data = json.loads(page.text)
            error = int(data['statusCode'])
            print(data['message'])
            time.sleep(3)
            # 3s一次
        except:
            get_data(jse_url,cookies)
    s.close()


if __name__ == '__main__':
    sno=input()
    password=input()
    url,cookies = signIN_sso(sno, password)
    # get_data(url,cookies)